/**
 * 
 */
package org.tio.sitexxx.all;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.flash.policy.server.FlashPolicyServerStarter;
import org.tio.sitexxx.im.server.TioSiteImServerStarter;
import org.tio.sitexxx.service.init.CacheInit;
import org.tio.sitexxx.service.init.JFInit;
import org.tio.sitexxx.service.init.JsonInit;
import org.tio.sitexxx.service.init.PropInit;
import org.tio.sitexxx.service.init.RedisInit;
import org.tio.sitexxx.service.init.ZkInit;
import org.tio.sitexxx.service.ip2region.Ip2RegionInit;
import org.tio.sitexxx.service.service.base.SensitiveWordsService;
import org.tio.sitexxx.service.service.conf.AvatarService;
import org.tio.sitexxx.view.TioSiteHttpServerViewStarter;
import org.tio.sitexxx.web.server.init.WebApiInit;
import org.tio.utils.Threads;
import org.tio.utils.jfinal.P;
import org.tio.utils.quartz.QuartzUtils;

/**
 * @author tanyaowu
 */
public class Starter {
	private static Logger log = LoggerFactory.getLogger(Starter.class);

	/**
	 * 
	 */
	public Starter() {
	}

	/**
	 * 基本的初始化，一般用于单元小测试
	 * @throws SQLException 
	 */
	public static void initBase() throws SQLException {

		PropInit.init();

		// redis初始化，里面会有topic等的初始化
		RedisInit.init();

		// ip2region初始化
		Ip2RegionInit.init();

		// 敏感词初始化
		SensitiveWordsService.init();

		// Json配置初始化
		JsonInit.init();

		// jfinal 初始化
		JFInit.init();

		// 缓存初始化
		CacheInit.init();

	}

	/**
	 * @param args
	 * @author tanyaowu
	 */
	public static void main(String[] args) {
		try {
			initBase();

			boolean startIm = true;//P.getInt("start.im", 1) == 1;
			boolean startView = true;//P.getInt("start.view", 1) == 1;
			boolean startFlashPolicyServer = P.getInt("start.flash.policy.server", 1) == 1;

			org.tio.sitexxx.web.server.init.TopicInit.init();

			// 加载头像数据
			AvatarService.loadData();

			if (startIm) {
				//先启动聊天服务器，再启动zk
				TioSiteImServerStarter.imServerInit();

				if (startFlashPolicyServer) {
					FlashPolicyServerStarter.start(null, null, Threads.getTioExecutor(), Threads.getGroupExecutor());
				}
			}

			// zookeeper初始化
			ZkInit.init(ZkClientDecorator.me);

			//			Zk.zkclient.getConnectionStateListenable().addListener(ZkConnectionStateListener.me);

			//			if (startStatws) {
			//				Zk.zkclient.getConnectionStateListenable().addListener(new ConnectionStateListener() {
			//					@Override
			//					public void stateChanged(CuratorFramework client, ConnectionState newState) {
			//						if (newState == ConnectionState.CONNECTED) {
			//							//创建相关节点
			//							try {
			//								String myip = org.tio.sitexxx.service.vo.Const.SERVICE_HOST;
			//								String mypath = Const.ZkNode.STAT_WS_SERVERS + "/" + myip;
			//								StatWsPerformanceDataVo statWsPerformanceDataVo = StatWs.createStatWsPerformanceDataVo();
			//								Zk.createOrUpdate(mypath, Json.toJson(statWsPerformanceDataVo), CreateMode.EPHEMERAL);
			//							} catch (Exception e) {
			//								log.error(e.toString(), e);
			//							}
			//						}
			//					}
			//				});
			//			}

			// 启动Api服务器
			WebApiInit.init();

			//启动定时任务
			QuartzUtils.start();

			//			FlashPolicyServerStarter.start(null, null, Threads.tioExecutor, Threads.groupExecutor);

			if (startIm) {
				org.tio.sitexxx.im.server.init.TopicInit.init();
			}

			//启动View服务器
			if (startView) {
				org.tio.sitexxx.view.init.TopicInit.init();

				TioSiteHttpServerViewStarter.initView(args);
			}

		} catch (Throwable e) {
			log.error(e.toString(), e);
			System.exit(1);
		}

	}

}
